<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/conf/global.config.php');
require_once(CONF_DIR.'db.config.php');
require_once(SRC_DIR.'util.php');

Util::Logger($_SERVER["REQUEST_URI"],"paycenter");

if (!isset($_GET['opid'],$_GET['account'],$_GET['money'],$_GET['channel'],$_GET['serverid'],$_GET['userid'],$_GET['actorid']))
{
	exit('-1');
}

$opid = $_GET['opid'];
$account = urldecode($_GET['account']);//账号名 spid_id 648yx_497557,跟globaluser.account表对应
$rmb = floatval($_GET['money']);
$money = $rmb*10;
$channel = $_GET['channel'];
$server = $_GET['serverid'];
$userid = $_GET['userid'];//accountid，游戏账号id，非渠道的
$actorid = $_GET['actorid'];
$spid = $channel;
$ip =  get_real_ip();

if(!in_array($ip,$AM_CONFIG['allowip']))
{
	exit('-2');   
}
if(strpos($account, $channel)== -1)
{
	$account = $channel."_".$account;
}
if (substr($server, 0, 1) == 'S') $server = substr($server, 1);
if(!is_numeric($money) || $money <= 0)
{
	exit('-3');   
}

$host = $AM_CONFIG['host'];
$am_key = $AM_CONFIG['amkey'];
$sign = "opid=$opid&account=$account&actorid=$actorid&money=$money&server=$server&key=".$am_key;
$sign = md5($sign);

$url = "http://$host/djrm/$spid/pay?";
$url .= "opid=$opid&account=".urlencode($account)."&actorid=$actorid&money=$money&server=$server&userid=$userid&sign=$sign&ram=$rmb&channel=".urlencode($channel);
$result = file_get_contents($url);

$gburl = "http://gb.dragongame.com/sdk/pay_callback/payorder.php?";
$gburl .= "opid=$opid&account=".urlencode($account)."&actorid=$actorid&money=$money&server=$server&userid=$userid&sign=$sign&ram=$rmb&channel=".urlencode($channel);
file_get_contents($gburl);

Util::Logger($url." result = ".$result,"paycenter");

if($result == "OK")
{
	exit('0');
}
else
{
	exit($result);
}


function get_real_ip()
{ 
	$ip=false; 
	if(!empty($_SERVER["HTTP_CLIENT_IP"]))
	{ 
		$ip = $_SERVER["HTTP_CLIENT_IP"]; 
	} 
	if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
	{ 
		$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']); 
		if ($ip) 
		{
			array_unshift($ips, $ip);
			$ip = FALSE; 
		} 
		for ($i = 0; $i < count($ips); $i++) 
		{ 
			if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) 
			{ 
				$ip = $ips[$i]; 
				break; 
			} 
		} 
	} 
	return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); 
} 

?>